package com.ydy.controller;

import com.ydy.constant.Const;
import com.ydy.enums.PayMethod;
import com.ydy.pojo.OrderStatus;
import com.ydy.pojo.bo.SubmitOrderBO;
import com.ydy.pojo.vo.PaymentInfoVO;
import com.ydy.resource.ServerUri;
import com.ydy.service.OrderService;
import com.ydy.utils.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "订单相关", tags = {"订单相关的api接口"})
@RequestMapping("orders")
@RestController
public class OrdersController {

    public static final Logger log =
            LoggerFactory.getLogger(OrdersController.class);

    @Autowired
    private OrderService orderService;

    @Autowired
    private ServerUri serverUri;

    /**
     * 用户下单
     * @param submitOrderBO
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "用户下单", notes = "用户下单", httpMethod = "POST")
    @PostMapping("/create")
    public ReturnResult create(
            @RequestBody SubmitOrderBO submitOrderBO,
            HttpServletRequest request,
            HttpServletResponse response) {

        if (submitOrderBO.getPayMethod() != PayMethod.WEIXIN.type
                && submitOrderBO.getPayMethod() != PayMethod.ALIPAY.type ) {
            return ReturnResult.errorMsg("支付方式不支持！");
        }

        // 1. 创建订单
        String orderId = orderService.createOrder(submitOrderBO);

        // 2. 创建订单以后，移除购物车中已结算（已提交）的商品
        /**
         * 1001
         * 2002 -> 用户购买
         * 3003 -> 用户购买
         * 4004
         */
        // TODO 整合redis之后，完善购物车中的已结算商品清除，并且同步到前端的cookie
//        CookieUtils.setCookie(request, response, FOODIE_SHOPCART, "", true);

        return ReturnResult.ok(orderId);
    }

    /**
     * 没有支付中心接口,直接返回金额和固定金额微信收款二维码
     * @param merchantUserId 支付用户
     * @param merchantOrderId 支付订单
     * @return
     */
    @ApiOperation(value = "查询下单金额和微信收款码", notes = "查询下单金额和微信收款码", httpMethod = "POST")
    @PostMapping("/getWXPayQRCode")
    public ReturnResult getWXPayQRCode(@RequestParam String merchantUserId, @RequestParam String merchantOrderId){

        log.info("没有支付中心接口,直接返回金额和固定金额微信收款二维码,用户id:{},订单id:{}",merchantUserId,merchantOrderId);
        //做一个异步方法,30s把这个订单状态随机改为已支付
        orderService.updateOrderStatus(merchantOrderId);
        PaymentInfoVO paymentInfoVO = new PaymentInfoVO();
        paymentInfoVO.setWxCodeUrl(serverUri.appendImagUrl(Const.WX_PAY_PATH));
        return ReturnResult.ok(paymentInfoVO);
    }

    /**
     * 没有支付中心接口,直接返回金额和固定金额支付宝收款二维码
     * @param merchantUserId 支付用户
     * @param merchantOrderId 支付订单
     * @return
     */
    @ApiOperation(value = "查询下单金额和支付宝收款码", notes = "查询下单金额和支付宝收款码", httpMethod = "POST")
    @PostMapping("/getAliPayQRCode")
    public ReturnResult getAliPayQRCode(@RequestParam String merchantUserId, @RequestParam String merchantOrderId){

        log.info("没有支付中心接口,直接返回金额和固定金额支付宝收款二维码,用户id:{},订单id:{}",merchantUserId,merchantOrderId);
        //做一个异步方法,30s把这个订单状态随机改为已支付
        orderService.updateOrderStatus(merchantOrderId);
        PaymentInfoVO paymentInfoVO = new PaymentInfoVO();
        paymentInfoVO.setAliCodeUrl(serverUri.appendImagUrl(Const.ALI_PAY_PATH));
        return ReturnResult.ok(paymentInfoVO);
    }

    /**
     * 查询订单状态
     * @param orderId 订单id
     * @return
     */
    @PostMapping("getPaidOrderInfo")
    public ReturnResult getPaidOrderInfo(String orderId) {
        OrderStatus orderStatus = orderService.queryOrderStatusInfo(orderId);
        return ReturnResult.ok(orderStatus);
    }

}
